package com.easysoft.framework.article.service;

import com.easysoft.commons.utils.DateTimeUtils;
import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.article.mapper.SysArticleMapperExtd;
import com.easysoft.framework.article.model.SysArticle;
import com.easysoft.framework.article.model.SysArticleQuery;
import com.easysoft.framework.article.model.SysArticleView;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.role.model.SysRoleView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文章模块
 * Created by try on 2017/6/29.
 *
 */
@Service("service.ArticleService")
public class ArticleService {

    @Autowired
    @Qualifier("mapper.SysArticleMapperExtd")
    private SysArticleMapperExtd sysArticleMapperExtd;

    /**
     * 删除文章列表
     * @param id
     * @return
     */
    @DataSource(DataSource.MASTER)
    public int deleteByPrimaryKey(String id){
        return sysArticleMapperExtd.deleteByPrimaryKey(id);
    }

    /**
     * 查询文章列表
     * @param id
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public SysArticle selectByPrimaryKey(String id){
        return sysArticleMapperExtd.selectByPrimaryKey(id);
    }


    /**
     * 更新文章内容
     * @param sysArticle
     * @return
     */
    @DataSource(DataSource.MASTER)
    public int updateByPrimaryKeyWithBLOBs(SysArticle sysArticle){
        return sysArticleMapperExtd.updateByPrimaryKeySelective(sysArticle);
    }

    /**
     * 获取文章列表
     * @param page
     * @param query
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<SysArticleView> selectSysArticleListByPage(Page page,SysArticleQuery query){

        int maxRow = sysArticleMapperExtd.selectSysArticleListCount(query);
        page.setMaxRow(maxRow);

        if (maxRow == 0) {
            return new ArrayList<SysArticleView>();
        }

        return sysArticleMapperExtd.selectSysArticleListByPage(page,query);
    }

    /**
     * 新增文章
     * @param sysArticle
     * @return
     */
    @DataSource(DataSource.MASTER)
    public int addByPrimaryKeyWithBLOBs(SysArticle sysArticle){

        sysArticle.setId(UUIDUtils.getUUID());
        sysArticle.setCreateTime(DateTimeUtils.getCurrentDate());

        return sysArticleMapperExtd.insertSelective(sysArticle);
    }


    public List<Map<String, Object>> selectSysArticle(int articleOrder,String categoryId) {
        return sysArticleMapperExtd.selectSysArticle(articleOrder,categoryId);
    }
}
